package client.view;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.swing.JFrame;

import client.remotes.ICommunicator;

import java.util.ArrayList;

import server.model.remotes.IAdmin;
import server.model.remotes.IMeeting;
import server.model.remotes.IMeetingService;

public class GuiManager implements WindowListener {
	Registry registry;	
	
	private IAdmin admin;
	private IMeeting meeting;	
	private IMeetingService meetingService;
	private String username;
	private JFrame frame;

	private UserInfo userInfo;
	private MainGui mainGui;
	private RegisterGui registerGui;
	private LoginGui loginGui;
	private MenuGui	menuGui;
	private CreateGroupGui createGroupGui;
	private CreateMeetingGui createMeetingGui;
	private CreateAnnouncementGui createAnnouncementGui;
	private ArrayList <ClientMeeting>clientMeetings;
	
	public GuiManager() {
		getAdminObject();
		createGuiObjects();
		clientMeetings = new ArrayList<ClientMeeting>();
		frame = new JFrame();

		frame.setVisible(true);
		frame.setSize(615, 400);
		frame.setLocation(312, 234);
		frame.addWindowListener(this);
	}

	public IAdmin getAdmin() {
		return admin;
	}
	
	public IMeetingService getMeetingService() {
		return meetingService;		
	}
	
	private void createGuiObjects() {
		mainGui = new MainGui(this);
		userInfo = new UserInfo(this);
		registerGui = new RegisterGui(this);		
		loginGui = new LoginGui(this);		
		createGroupGui = new CreateGroupGui(this);		
		createMeetingGui = new CreateMeetingGui(this);
		menuGui = new MenuGui(this);
		createAnnouncementGui = new CreateAnnouncementGui(this);
	}

	public void switchScreen(String view) {
		frame.getContentPane().removeAll();
		
		if (view == "mainGui") {
			frame.getContentPane().add(mainGui);
		} else if (view == "registerGui") {
			registerGui.reset();
			frame.getContentPane().add(registerGui);
		}else if (view == "loginGui"){
			loginGui.reset();
			frame.getContentPane().add(loginGui);
		}else if (view == "menuGui") {
				frame.getContentPane().add(menuGui);
				menuGui.reset();
		} else if (view == "createGroupGui") {
			createGroupGui.reset();
			frame.getContentPane().add(createGroupGui);
		}else if (view == "createMeetingGui"){
			createMeetingGui.reset();
			frame.getContentPane().add(createMeetingGui);			
		}else if (view == "createAnnouncementGui"){
			createAnnouncementGui.reset();
			frame.getContentPane().add(createAnnouncementGui);
		}else if (view == "UserInfo"){
			userInfo.reset();
			frame.getContentPane().add(userInfo);
		}
		
		
		frame.getContentPane().repaint();
	}
	
	private void getAdminObject(){
		Registry registry;

		try {
			registry = LocateRegistry.getRegistry(Registry.REGISTRY_PORT);

			try {
				admin = (IAdmin) registry.lookup("virtualRoomReg");			
				
			} catch (NotBoundException e) {				
				e.printStackTrace();
			}

		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
	
	public void setMeetingService(IMeetingService meetingService) {
		this.meetingService = meetingService;
		userInfo = this.meetingService.getUserInfo();
		
	}
	
	public void windowClosing(WindowEvent arg0) {
		System.exit(0);
	}
	
	public void windowOpened(WindowEvent arg0) {}	
	public void windowActivated(WindowEvent arg0) {}
	public void windowClosed(WindowEvent arg0) {}
	public void windowDeactivated(WindowEvent arg0) {}
	public void windowDeiconified(WindowEvent arg0) {}
	public void windowIconified(WindowEvent arg0) {}

	public void attendMeeting(String name) {			
		try {
			IMeeting meeting;
			meeting = meetingService.attendMeeting(name);
			ClientMeeting clientMeeting = new ClientMeeting(username, meeting);
			ICommunicator ic = clientMeeting.getCommunicator();
			meeting.addCommunicator(ic);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	public void setUserName(String name) {
		username = name;
		
	}
	
}
